package com.example.certificate.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.annoatiton.CurrentUser;
import com.example.certificate.annoatiton.LoginToken;
import com.example.certificate.dto.BasePageDTO;
import com.example.certificate.dto.CustomerDTO;
import com.example.certificate.dto.UpdateCusDTO;
import com.example.certificate.entity.CustomerAddr;
import com.example.certificate.entity.User;
import com.example.certificate.execption.BusinessException;
import com.example.certificate.service.CustomerAddrService;
import com.example.certificate.utils.ResultEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hao
 * @since 2022-03-10 09:54:48
 */
@RestController
@RequestMapping("/customerAddr")
public class CustomerAddrController {

    @Autowired
    private CustomerAddrService customerAddrService;

    @LoginToken
    @ApiOperation("添加用户收货地址")
    @PostMapping(value = "/addCustomerAddress")
    public ResultEntity addCustomer(@Validated(value = CustomerDTO.Customer.class)@RequestBody CustomerDTO dto,
                                    @ApiIgnore @CurrentUser User user){
        customerAddrService.addCustomer(dto,user);
        return new ResultEntity();
    }


    @LoginToken
    @ApiOperation("查询用户收货地址")
    @PostMapping(value = "/selCustomer")
    public ResultEntity addCustomer(@RequestBody BasePageDTO dto, @ApiIgnore @CurrentUser User user){
        Page<CustomerAddr> page=new Page<CustomerAddr>();
        page.setCurrent(dto.getCurrent()==null?1:dto.getCurrent());
        page.setSize(dto.getPageSize()==null?10:dto.getPageSize());
        return new ResultEntity(customerAddrService.selCustomer(page,user));
    }


    @LoginToken
    @ApiOperation("修改用户的收货地址")
    @PostMapping(value = "/updateCus")
    public ResultEntity updateCus(@RequestBody UpdateCusDTO dto, @ApiIgnore @CurrentUser User user) throws BusinessException {
        customerAddrService.updateCus(dto,user);
        return new ResultEntity();
    }


    @LoginToken
    @ApiOperation("删除用户收货地址")
    @PostMapping(value = "/delCus")
    public ResultEntity delCus(@RequestBody UpdateCusDTO dto,@ApiIgnore @CurrentUser User user){
        customerAddrService.delCus(dto,user);
        return new ResultEntity();
    }


    @LoginToken
    @ApiOperation(("查询用户正在使用的地址"))
    @PostMapping(value = "/selUserAddress")
    public ResultEntity selUserAddress(@ApiIgnore @CurrentUser User user){
        return  new ResultEntity(customerAddrService.selUserAddress(user));
    }
}

